package com.ucar.train.web;

import com.github.pagehelper.PageInfo;
import com.ucar.train.dto.MenuDTO;
import com.ucar.train.dto.RoleAndMenuDTO;
import com.ucar.train.dto.RoleDTO;
import com.ucar.train.service.impl.MenuServiceImpl;
import com.ucar.train.service.impl.RoleServiceImpl;
import com.ucar.train.utils.GsonUtils;
import com.ucar.train.vo.Role;
import com.ucar.train.vo.RoleAndMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author zhoujinmu
 * @title RoleController
 * @projectName train-parent
 * @description 角色的增删改查，以及角色对应权限的增删改查
 * @created 2019-07-24 12:25
 **/
@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;

    @Autowired
    private MenuServiceImpl menuService;

  @RequestMapping("/showRole")
  public ModelAndView getRoleList(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size, @RequestParam(name = "orderBy", required = true, defaultValue = "id asc") String orderBy, HttpServletRequest httpServletRequest){
    ModelAndView modelAndView = new ModelAndView();
    List<RoleDTO> roles = roleService.getRoleList(page,size,orderBy);
    HttpSession httpSession = httpServletRequest.getSession();
    httpSession.setAttribute("orderBy",orderBy);
    PageInfo pageInfo = new PageInfo(roles);
    modelAndView.addObject("pageInfo",pageInfo);
    modelAndView.setViewName("roles-list");
    return modelAndView;
  }

    @RequestMapping("/addRoleAndMenus")
    public String addRoleAndMenus(@RequestParam("rid")Long rid,@RequestParam("mid")Long mid){
        System.out.println(rid);
        roleService.addMenuToRole(rid,mid);
        return "redirect:getRoleAndMenu";
    }

    @RequestMapping("/updateRoleAndMenus")
    public String updateRoleAndMenus(@RequestParam("rid")Long rid,@RequestParam("mid")Long mid){
        roleService.updateRoleAndMenu(rid,mid);
        return "redirect:showRole";
    }

    @RequestMapping("/deleteMenuForRole")
    public String deleteMenuForRole(@RequestParam("rid")Long rid,@RequestParam("mid")Long mid){
        roleService.deleteMenuForRole(rid,mid);
        return "redirect:getRoleAndMenu";
    }

    @RequestMapping("/deleteRole")
    public String deleteRole(@RequestParam("rid")Long rid){
        roleService.deleteRole(rid);
        return "redirect:showRole";
    }

    @RequestMapping("/addRole")
    public String getRoleList(Role role,HttpServletRequest httpServletRequest){
      HttpSession httpSession = httpServletRequest.getSession();
      String createOperator = (String)httpSession.getAttribute("name");
      roleService.addRole(role,createOperator);
      return "redirect:showRole";
    }

    @RequestMapping("/updateRole")
    public String updateRole(Role role, HttpServletRequest httpServletRequest){
      HttpSession httpSession = httpServletRequest.getSession();
      String modifyOperator = (String)httpSession.getAttribute("name");
      roleService.updateRole(role,modifyOperator);
      return "redirect:showRole";
    }

    @RequestMapping("/getRoleAndMenu")
    public ModelAndView getRoleAndMenu(){
        ModelAndView modelAndView = new ModelAndView();
        List<RoleAndMenuDTO> roleAndMenus = roleService.getRoleAndMenu();
        modelAndView.addObject("roleAndMenus",roleAndMenus);
        modelAndView.setViewName("permission");
        return modelAndView;

    }

    @RequestMapping("/getRoleById")
    public ModelAndView getRoleById(@RequestParam("id")Long id){
        ModelAndView modelAndView = new ModelAndView();
        RoleDTO role = roleService.getRoleById(id);
        System.out.println(role);
        modelAndView.addObject("role",role);
        modelAndView.setViewName("role-update");
        return modelAndView;
    }

    @RequestMapping("/selectPermission")
    public ModelAndView selectPermission(@RequestParam("rid")Long rid,HttpServletRequest httpServletRequest){
      System.out.println(rid);
      HttpSession httpSession = httpServletRequest.getSession();
      httpSession.setAttribute("roleId",rid);
      ModelAndView modelAndView = new ModelAndView();
      List<MenuDTO> permissions = roleService.selectPermission(rid);
      modelAndView.addObject("permissions",permissions);
      modelAndView.setViewName("permission-add");
      return modelAndView;
    }

    @RequestMapping("/getByGroupId")
  public String getByGroupId(@RequestParam("groupId")Long groupId){
      return GsonUtils.toJson(roleService.getByGroupId(groupId));
    }

  @RequestMapping("/getByNopermission")
  public ModelAndView getByGroupId(ModelAndView modelAndView){
    List<RoleDTO> roles = roleService.getNoPermission();
    List<MenuDTO> menus = menuService.listMenus();
    System.out.println(roles);
    System.out.println(menus);
    modelAndView.addObject("roles",roles);
    modelAndView.addObject("permissoins",menus);
    modelAndView.setViewName("permission-new");
    return modelAndView;
  }
}
